using FarseerGames.GettingStarted.Demos.Demo1;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Net;
using System;
using Microsoft.Xna.Framework.GamerServices;
using NetPong;

namespace FarseerGames.GettingStarted.ScreenSystem
{

    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    internal class MainMenuScreen : MenuScreen
    {
        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        ///
        string errorMessage;
        public MainMenuScreen()
        {
            MenuEntries.Add("Create Network Session");
            MenuEntries.Add("Join Network Session");
            MenuEntries.Add("Exit");
            LeftBorder = 100;
        }
        /// <summary>
        /// Starts hosting a new network session.
        /// </summary>
        NetworkSession CreateSession()
        {
            NetworkSession net = NetworkSession.Create(NetworkSessionType.SystemLink, 1, 31);
            net.AllowJoinInProgress = true;
            return net;
        }


        /// <summary>
        /// Joins an existing network session.
        /// </summary>
        NetworkSession JoinSession()
        {
            try
            {
                // Search for sessions.
                using (AvailableNetworkSessionCollection availableSessions =
                            NetworkSession.Find(NetworkSessionType.SystemLink,
                                                1, null))
                {
                    if (availableSessions.Count == 0)
                    {
                        errorMessage = "No network sessions found.";
                        return null;
                    }

                    // Join the first session we found.
                    NetworkSession net = NetworkSession.Join(availableSessions[0]);
                    return net;
                }
            }
            catch (Exception e)
            {
                errorMessage = e.Message;
            }
            return null;
        }

        /// <summary>
        /// Responds to user menu selections.
        /// </summary>
        protected override void OnSelectEntry(int entryIndex)
        {
            NetworkSession net;
            switch (entryIndex)
            {
                case 0:
                    net = CreateSession();
                    ScreenManager.AddScreen(new PongScreen(net));
                    break;
                case 1:
                    net = JoinSession();
                    ScreenManager.AddScreen(new PongScreen(net));
                    break;
                default:
                    ScreenManager.Game.Exit();
                    break;
            }
            ExitScreen();
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            /* ScreenManager.SpriteBatch.DrawString(ScreenManager.SpriteFonts.DiagnosticSpriteFont,
                                                 "1) Toggle between debug and normal view using either F1 on the keyboard or 'Y' on the controller",
                                                 new Vector2(100, ScreenManager.ScreenHeight - 116), Color.White);
            ScreenManager.SpriteBatch.DrawString(ScreenManager.SpriteFonts.DiagnosticSpriteFont,
                                                 "2) Keyboard users, use arrows and enter to navigate menus",
                                                 new Vector2(100, ScreenManager.ScreenHeight - 100), Color.White); */
            base.Draw(gameTime);
            ScreenManager.SpriteBatch.End();
        }
    }
}